package com.ruoyi.web.controller.aggregation;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.aggregation.dto.PageDTO;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.aggregation.Point;
import com.ruoyi.system.service.aggregation.IPointService;

/**
 * 分佣明细Controller
 * 
 * @author ruoyi
 * @date 2025-01-13
 */
@RestController
@RequestMapping("/agg/point")
public class PointController
{
    @Autowired
    private IPointService pointService;

    /**
     * 分页查询分佣明细
     */
    @PostMapping("/page")
    public AjaxResult page(@RequestBody PageDTO pageDTO)
    {
        try
        {
            PageHelper.startPage(pageDTO.getPageIndex().intValue(), pageDTO.getPageSize().intValue());
            
            Point query = new Point();
            // 如果传了userId，查询指定用户的分佣明细
            if (pageDTO.getUserId() != null) {
                query.setUserId(pageDTO.getUserId());
            }
            // 如果传了平台类型，筛选广告分佣（穿山甲/taku）还是异业分佣
            if (pageDTO.getPlatform() != null) {
                query.setPlatform(pageDTO.getPlatform());
            }
            
            List<Point> list = pointService.selectPointList(query);
            PageInfo<Point> pageInfo = new PageInfo<>(list);
            return AjaxResult.success(pageInfo);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询当前用户的分佣明细
     */
    @PostMapping("/myPage")
    public AjaxResult myPage(@RequestBody PageDTO pageDTO)
    {
        try
        {
            Long currentUserId = SecurityUtils.getUserId();
            if (currentUserId == null) {
                return AjaxResult.error("用户未登录");
            }
            
            PageHelper.startPage(pageDTO.getPageIndex().intValue(), pageDTO.getPageSize().intValue());
            
            Point query = new Point();
            query.setUserId(currentUserId);
            // 如果传了平台类型，筛选广告分佣还是异业分佣
            if (pageDTO.getPlatform() != null) {
                query.setPlatform(pageDTO.getPlatform());
            }
            
            List<Point> list = pointService.selectPointList(query);
            PageInfo<Point> pageInfo = new PageInfo<>(list);
            return AjaxResult.success(pageInfo);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取分佣明细详情
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(@RequestParam Long id)
    {
        try
        {
            Point point = pointService.selectPointById(id);
            return AjaxResult.success(point);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取分佣统计（按平台分组）
     */
    @GetMapping("/statistics")
    public AjaxResult statistics(@RequestParam(required = false) Long userId)
    {
        try
        {
            Long targetUserId = userId;
            if (targetUserId == null) {
                // 没传userId，查询当前登录用户
                targetUserId = SecurityUtils.getUserId();
                if (targetUserId == null) {
                    return AjaxResult.error("用户未登录");
                }
            }
            
            // TODO: 实现统计逻辑，返回各平台的分佣总额
            // 可以调用service的统计方法
            
            return AjaxResult.success("统计功能待实现");
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}

